//
//  CustomCell.m
//  仿QQ聊天UDPSocketDemo
//
//  Created by liuyuecheng on 16/4/25.
//  Copyright © 2016年 liuyuecheng. All rights reserved.
//

#import "CustomCell.h"

@implementation CustomCell
{
    UIImageView *_imageView;
    UILabel *_label;
    UIImageView *_HeadimageView;
    UIButton *_timebutton;
    UILabel *_titlelabel;
    UIImageView *_screenshotimage;

}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        [self.contentView addSubview:_imageView];
        
        _label = [[UILabel alloc]initWithFrame:CGRectMake(5, 20, 0, 0)];
        _label.font = [UIFont systemFontOfSize:15];
        _label.numberOfLines = 0;
        [_imageView addSubview:_label];
        
        
       _timebutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_timebutton setTintColor:[UIColor whiteColor]];
        [_timebutton setBackgroundImage:[UIImage imageNamed:@"时间"] forState:UIControlStateNormal];
        [_timebutton setFont:[UIFont systemFontOfSize:14]];
        [self.contentView addSubview:_timebutton];

        
        _titlelabel = [[UILabel alloc]init];
        [_titlelabel setFont:[UIFont systemFontOfSize:14]];
        _titlelabel.textColor =  [UIColor whiteColor];
        [self.contentView addSubview:_titlelabel];

        
        _HeadimageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_HeadimageView];
        
        
        _screenshotimage = [[UIImageView alloc]init];
        [_imageView addSubview:_screenshotimage];
        
        
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
    
    
    
}

- (void)setModel:(ChatModel *)model
{
    _model = model;
    
    //将内容放到label上,设置label的宽高
    _label.text = model.content;
    _label.frame = CGRectMake(10, 10, model.contentSize.width, model.contentSize.height);
    
    UIImage *image = nil;
    
    
    //判断是自己发的,就放右侧.别人发的,放左侧
    if ([model.type isEqualToString:@"me"])
    {
        
            if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
            _timebutton.frame =CGRectMake(self.size.width/2-25, 10, 50, 20);
        }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){
            _timebutton.frame =CGRectMake(375/2-25, 10, 50, 20);
        }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")){
            _timebutton.frame =CGRectMake(414/2-25, 10, 50, 20);
        }
        
        _HeadimageView.frame =CGRectMake(7, 40, 50, 50);
        _titlelabel.frame =CGRectMake(24, 95, 100, 20);
        
        if ([model.typeimage isEqualToString:@"me"]) {
        
            _screenshotimage.frame = CGRectMake(64, 40, 120, 140);
            _screenshotimage.image =image;
            _screenshotimage.image = [UIImage imageWithData:model.image];
        }else{
        
            _imageView.frame = CGRectMake(62 , 55, self.model.contentSize.width + 20, self.model.contentSize.height + 20);
            UIImage *rightImage = [UIImage imageNamed:@"我-26"];
            //拉伸,保留左侧20个像素,上面15个像素不变
            image = [rightImage stretchableImageWithLeftCapWidth:20 topCapHeight:15];
            

        }

        
        //左侧
        _HeadimageView.image = [UIImage imageNamed:@"我"];
        _titlelabel.text = @"我";
        
        
    }else if ([model.type isEqualToString:@"kefu"]) {

        if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
            _timebutton.frame =CGRectMake(self.size.width/2-25, 10, 50, 20);
            _HeadimageView.frame =CGRectMake(self.size.width-60, 40, 50, 50);
            _titlelabel.frame = CGRectMake(self.size.width-50, 95, 100, 20);
            if ([model.typeimage isEqualToString:@"kefu"]) {
                
                _screenshotimage.frame = CGRectMake(self.size.width-187, 40, 120, 140);
                _screenshotimage.image = [UIImage imageNamed:@"我"];
                
            }else{
                
                _imageView.frame = CGRectMake(265 - self.model.contentSize.width - 30 , 55, self.model.contentSize.width + 20, self.model.contentSize.height +20);
            }

            
        }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){
            
            _timebutton.frame =CGRectMake(375/2-25, 10, 50, 20);
            _HeadimageView.frame =CGRectMake(375-60, 40, 50, 50);
            _titlelabel.frame = CGRectMake(375-50, 95, 100, 20);
            
            if ([model.typeimage isEqualToString:@"kefu"]) {
                
                _screenshotimage.frame = CGRectMake(375-187, 40, 120, 140);
//                _screenshotimage.image = [UIImage imageNamed:@"我"];
                
            }else{
                
                _imageView.frame = CGRectMake(320 - self.model.contentSize.width - 30 , 55, self.model.contentSize.width + 20, self.model.contentSize.height +20);
            }

            
        }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")){
            
            _timebutton.frame =CGRectMake(414/2-25, 10, 50, 20);
            _HeadimageView.frame =CGRectMake(414-60, 40, 50, 50);
            _titlelabel.frame = CGRectMake(414-50, 95, 100, 20);
            
            if ([model.typeimage isEqualToString:@"kefu"]) {
                
                _screenshotimage.frame = CGRectMake(414-187, 40, 120, 140);
                _screenshotimage.image = [UIImage imageNamed:@"我"];
                
            }else{
                
                _imageView.frame = CGRectMake(359 - self.model.contentSize.width - 30 , 55, self.model.contentSize.width + 20, self.model.contentSize.height +20);
            }
            
        }

        //右侧
        UIImage *leftImage = [UIImage imageNamed:@"形状-26"];
        _HeadimageView.image = [UIImage imageNamed:@"客服"];
        _titlelabel.text = @"客服";

        //拉伸,保留左侧20个像素,上面15个像素不变
        image = [leftImage stretchableImageWithLeftCapWidth:20 topCapHeight:15];
        
    }
    //将拉伸好了的图片,设置
    _imageView.image = image;
        
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
